package servlet;

import dao.NewDao;
import entity.New;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/newList")
public class NewPageDataServlet extends HttpServlet {
	
	@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doGet(request, response);
    }
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
		final int SIZE = 6;
		String title = request.getParameter("selectTitle");
		String author = request.getParameter("selectAuthor");
		String startTime = request.getParameter("selectStartTime");
		String endTime = request.getParameter("selectEndTime");
		StringBuilder condition = new StringBuilder();
		StringBuilder urlParams =new StringBuilder();
		ArrayList<Object> parmas = new ArrayList<>();
		condition.append("1=1");
		if(title != null && !title.isEmpty()){
			System.out.println(title);
			condition.append(" and title like ?");
			parmas.add("%" + title + "%");
			urlParams.append("&selectTitle=" + URLEncoder.encode(title,"utf-8"));
		}
		if (author != null && !author.isEmpty()){
			System.out.println(author);
			condition.append(" and author like ?");
			parmas.add("%" + author + "%");
			urlParams.append("&selectAuthor=" + URLEncoder.encode(author,"utf-8"));
		}
		if (startTime != null && !startTime.isEmpty() && endTime != null && !endTime.isEmpty()){
			System.out.println(startTime + "-" + endTime);
			condition.append(" and put_time between ? and ?");
			parmas.add(startTime);
			parmas.add(endTime);
			urlParams.append("&selectStartTime=" + URLEncoder.encode(startTime,"utf-8"));
			urlParams.append("&selectEndTime=" + URLEncoder.encode(endTime,"utf-8"));
		}
		System.out.println(urlParams);
		Integer page = null;
		try {
			page = Integer.parseInt(request.getParameter("page"));	
		} catch (Exception e) {
			page = 1;
		}
		int start = (page - 1)*SIZE;
		NewDao dao = new NewDao();
		List<New> list = dao.queryNewPage(start, SIZE, condition.toString(), parmas);
		int total = dao.total_news(condition.toString(), parmas);
		int pageNum = total / SIZE;
		if (total % SIZE != 0) {
			pageNum += 1;
		}
		StringBuilder sb = new StringBuilder(); 
		for (int i = 1; i <= pageNum; i++) {
			String str = "<a href=\"#url\">第" + i + "页</a>&nbsp;&nbsp;";
			str = str.replace("#url", request.getContextPath() + "/newList?page=" + i + urlParams.toString());
			if (page == i) {
				str = "第" + i + "页&nbsp;&nbsp;";
			}
			sb.append(str);
		}
		System.out.println(sb);
		request.setAttribute("page", sb.toString());
		request.setAttribute("news", list);
		request.getRequestDispatcher("./newsManage.jsp").forward(request, response);
	}


}
